//
//  YFBuyCountViewCell.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFBuyCountViewCell.h"

@interface YFBuyCountViewCell()

@property (nonatomic, strong) UILabel *countLabel;
@end

@implementation YFBuyCountViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"width = %f, height= %f", frame.size.width, frame.size.height);
        self.buyCount = 1;
        [self makeView];
    }
    return self;
}

- (void)makeView {
    
//    self.backgroundColor = [UIColor blueColor];
    
    CGFloat btnWH = 25;
    // 减
    UIButton *reduceButton = [[UIButton alloc] init];
    reduceButton.frame = CGRectMake(0, 0, btnWH, btnWH);
    [reduceButton addTarget:self action:@selector(reduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [reduceButton setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
//    reduceButton.layer.borderWidth = 1;
//    reduceButton.layer.borderColor = YFColor_Text.CGColor;
    reduceButton.backgroundColor = YFColor_Background;
    [self addSubview:reduceButton];
    
    // 购买数目
    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(btnWH, 0, btnWH, btnWH)];
    countLabel.text = [NSString stringWithFormat:@"%zd", self.buyCount];
    countLabel.font = [UIFont systemFontOfSize:12];
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.textColor = YFColor_Text;
    [self addSubview:countLabel];
    self.countLabel = countLabel;
    
    // 加
    UIButton *plusButton = [[UIButton alloc] init];
    plusButton.frame = CGRectMake(btnWH*2, 0, btnWH, btnWH);
    [plusButton addTarget:self action:@selector(plusButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [plusButton setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
//    plusButton.layer.borderWidth = 1;
//    plusButton.layer.borderColor = YFColor_Text.CGColor;
    plusButton.backgroundColor = YFColor_Background;
    [self addSubview:plusButton];
}

- (void)reduceButtonClick {
    self.buyCount--;
    if (self.buyCount < 1) {
        self.buyCount = 1;
        [MBPHUD showAlertWithTitle:@"商品数量不可为0!"];
    }else {
        
        self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.buyCount];
        if (self.didSelectedBlock) {
            self.didSelectedBlock(self.buyCount);
        }
    }
}

- (void)plusButtonClick {
    self.buyCount++;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", (long)self.buyCount];
    
    if (self.didSelectedBlock) {
        self.didSelectedBlock(self.buyCount);
    }
}

@end
